package org.example.servlet.stuinfo;

import com.google.gson.Gson;
import org.example.dao.StuinfoDao;
import org.example.entity.BaseResponse;
import org.example.entity.Stuinfo;
import org.example.util.RequestUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/edit.stuinfo")
public class UpdatStuinfoServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json");

        String params = RequestUtil.getRequestParameterStr(req);
        Gson gson = new Gson();
        Stuinfo stuinfo = gson.fromJson(params, Stuinfo.class);

        int row = StuinfoDao.updateStuinfo(stuinfo);

        BaseResponse baseResponse = new BaseResponse();
        if (row > 0){
            baseResponse.setCode(200);
            baseResponse.setMsg("修改成功");
        }else {
            baseResponse.setCode(600);
            baseResponse.setMsg("修改");
        }

        String json = gson.toJson(baseResponse);

        PrintWriter writer = resp.getWriter();
        writer.write(json);
        // 刷新页面
        writer.flush();
    }
}
